/*
 * User: rant
 * Date: 12/12/2006
 *
 *
 */
package laMalinche.testingPlayGround.flow;

import laMalinche.propertyChangeAdapters.model.OgnlHolder;
import org.jetbrains.annotations.NotNull;

import java.util.*;

/**
 *
 */
public class BasicFlowNode implements FlowNode {
	private final List<BasicFlowNode> children = new ArrayList<BasicFlowNode>();
	private Collector state;
	private String name;
	private Navigator navigator;

	public BasicFlowNode(final Collector collector) {
        state = collector;
	}

	public Collector getState() {
		return state;
	}

	public void setState(Collector state) {
		this.state = state;
	}

	public void addChangableField(String field) {
		state.addField(field);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public BasicFlowNode(String name, final OgnlHolder model) {
		this.name = name;
        state = new Collector();
		state.setName(name);
		state.setModel(model);
	}

	public List<BasicFlowNode> getChildren() {
		return children;
	}

	public void addNode(BasicFlowNode node) {
		children.add(node);
	}

	public BasicFlowNode getNextChild() {
		BasicFlowNode nextNode = null;
			if (navigator == null && !children.isEmpty()) {
				nextNode = children.get(0);
			} else if (navigator != null) {
				Collector collector = navigator.navigate(getState());
				if (collector != null) {
					nextNode = FlowNodeFactory.getInstance().getFlowNode(collector);
				}
			}

		return nextNode;
	}



	private BasicFlowNode getChildByName(@NotNull String nodeName) {
		for (BasicFlowNode node : children) {
			if (node.getName().equalsIgnoreCase(nodeName)) {
				return node;
			}
		}
		return null;
	}

	public void addNode(BasicFlowNode car, String transition) {
		throw new RuntimeException("Implement");
	}

	public void addNavigator(Navigator navigator) {
		this.navigator = navigator;
	}

	public Navigator getNavigator() {
		return navigator;
	}

	public Collection<? extends BasicFlowNode> getPossibleChildren() {
			return new ArrayList<BasicFlowNode>();
	}
}